package com.travel.mapper.content;

import com.travel.model.entity.content.Comment;
import com.travel.model.entity.content.Post;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Optional;
//文章mapper 查询文章 插入文章 更新文章 删除文章 
@Mapper
public interface PostMapper {
    
    @Select("SELECT p.*, u.nickname as user_nickname, u.avatar as user_avatar, u.status as user_status " +
            "FROM posts p " +
            "LEFT JOIN users u ON p.user_id = u.id " +
            "WHERE p.id = #{id}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "title", column = "title"),
        @Result(property = "content", column = "content"),
        @Result(property = "coverImage", column = "cover_image"),
        @Result(property = "city", column = "city"),
        @Result(property = "category", column = "category"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "status", column = "status"),
        @Result(property = "viewCount", column = "view_count"),
        @Result(property = "createdTime", column = "created_time"),
        @Result(property = "updatedTime", column = "updated_time"),
        @Result(property = "user.id", column = "user_id"),
        @Result(property = "user.nickname", column = "user_nickname"),
        @Result(property = "user.avatar", column = "user_avatar"),
        @Result(property = "user.status", column = "user_status")
    })
    Optional<Post> findById(@Param("id") Long id);
    
    @Select("SELECT p.*, u.nickname as user_nickname, u.avatar as user_avatar, u.status as user_status " +
            "FROM posts p " +
            "LEFT JOIN users u ON p.user_id = u.id " +
            "WHERE p.status = 1 " +
            "ORDER BY p.created_time DESC")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "title", column = "title"),
        @Result(property = "content", column = "content"),
        @Result(property = "coverImage", column = "cover_image"),
        @Result(property = "city", column = "city"),
        @Result(property = "category", column = "category"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "status", column = "status"),
        @Result(property = "viewCount", column = "view_count"),
        @Result(property = "createdTime", column = "created_time"),
        @Result(property = "updatedTime", column = "updated_time"),
        @Result(property = "user.id", column = "user_id"),
        @Result(property = "user.nickname", column = "user_nickname"),
        @Result(property = "user.avatar", column = "user_avatar"),
        @Result(property = "user.status", column = "user_status")
    })
    List<Post> findAll();
    
    @Select("SELECT p.*, " +
            "u.nickname as user_nickname, " +
            "u.avatar as user_avatar, " +
            "u.status as user_status " +
            "FROM posts p " +
            "LEFT JOIN users u ON p.user_id = u.id " +
            "WHERE p.user_id = #{userId} " +
            "ORDER BY p.created_time DESC")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "title", column = "title"),
        @Result(property = "content", column = "content"),
        @Result(property = "coverImage", column = "cover_image"),
        @Result(property = "city", column = "city"),
        @Result(property = "category", column = "category"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "status", column = "status"),
        @Result(property = "viewCount", column = "view_count"),
        @Result(property = "createdTime", column = "created_time"),
        @Result(property = "updatedTime", column = "updated_time"),
        @Result(property = "user.id", column = "user_id"),
        @Result(property = "user.nickname", column = "user_nickname"),
        @Result(property = "user.avatar", column = "user_avatar"),
        @Result(property = "user.status", column = "user_status")
    })
    List<Post> findByUserId(@Param("userId") Long userId);
    
    @Select("SELECT * FROM posts WHERE city = #{city}")
    List<Post> findByCity(@Param("city") String city);
    
    @Select("SELECT * FROM posts WHERE city = #{city} AND category = #{category}")
    List<Post> findByCityAndCategory(@Param("city") String city, @Param("category") String category);
    
    @Select("SELECT * FROM posts WHERE destination_id = #{destinationId}")
    List<Post> findByDestinationId(@Param("destinationId") Long destinationId);
    
    @Insert("INSERT INTO posts (title, cover_image, content, category, city, user_id, status, created_time) " +
            "VALUES (#{title}, #{coverImage}, #{content}, #{category}, #{city}, #{userId}, #{status}, #{createdTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Post post);
    
    @Update("UPDATE posts SET title = #{title}, cover_image = #{coverImage}, content = #{content}, " +
            "category = #{category}, city = #{city}, status = #{status}, updated_time = #{updatedTime} " +
            "WHERE id = #{id}")
    void update(Post post);
    
    @Update("UPDATE posts SET status = #{status} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);
    
    @Delete("DELETE FROM posts WHERE id = #{id}")
    int delete(Post post);
    
    @Select("SELECT * FROM posts WHERE status = #{status}")
    List<Post> findByStatus(@Param("status") Integer status);
    
    @Select("SELECT * FROM posts WHERE title LIKE CONCAT('%', #{keyword}, '%') OR content LIKE CONCAT('%', #{keyword}, '%')")
    List<Post> searchPosts(@Param("keyword") String keyword);
    
    @Select("SELECT * FROM posts ORDER BY view_count DESC")
    List<Post> findHotPosts();
    
    @Select("SELECT * FROM posts ORDER BY created_time DESC")
    List<Post> findLatestPosts();
    
    @Select("SELECT c.*, " +
            "u.nickname as user_nickname, " +
            "u.avatar as user_avatar, " +
            "u.status as user_status " +
            "FROM comments c " +
            "LEFT JOIN users u ON c.user_id = u.id " +
            "WHERE c.target_id = #{postId} " +
            "AND c.target_type = 'POST' " +
            "AND c.status = 1")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "content", column = "content"),
        @Result(property = "userId", column = "user_id"),
        @Result(property = "targetId", column = "target_id"),
        @Result(property = "targetType", column = "target_type"),
        @Result(property = "rating", column = "rating"),
        @Result(property = "status", column = "status"),
        @Result(property = "createdTime", column = "created_time"),
        @Result(property = "updatedTime", column = "updated_time"),
        @Result(property = "user.id", column = "user_id"),
        @Result(property = "user.nickname", column = "user_nickname"),
        @Result(property = "user.avatar", column = "user_avatar"),
        @Result(property = "user.status", column = "user_status")
    })
    List<Comment> findPostComments(@Param("postId") Long postId);
} 